//
//  HomeViewController.swift
//  MyWeiBlog
//
//  Created by laichunhui on 15/9/28.
//  Copyright © 2015年 lch. All rights reserved.
//

import UIKit

class HomeViewController: UIViewController, TitleHeaderViewDelegate
{
    /* titleView */
    private var itemTitleView: TitleHeaderView!
    
    /* contentView */
    private var contentView: UIScrollView!

    override func viewDidLoad()
    {
        super.viewDidLoad()
     
        //设置子控制器
        setupChildViewControllers()
        
        //设置导航栏
        setupNavigationBar()
        
        //设置容器内容
        setupContentView()
    }
    
    
// MARK: - 设置子控制器
    
    private func setupChildViewControllers() {
        //精选
        let essenceVc = FriendsBlogViewController()
        essenceVc.title = "精选"
        essenceVc.view.frame = ScreenBounds
        addChildViewController(essenceVc)
        
        //关注
        let FriendsVc = FriendsViewController()
        FriendsVc.title = "关注"
        FriendsVc.view.frame = ScreenBounds
        addChildViewController(FriendsVc)
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", highlImageName: "navigationbar_pop_highlighted", targer: self, action: "scanQRCode")
    }
    /// 扫描二维码
    func scanQRCode() {
        let sb = UIStoryboard(name: "QRCodeViewController", bundle: nil)
        presentViewController(sb.instantiateInitialViewController()!, animated: true, completion: nil)
    }
    
//MARK:- 设置导航栏
    private func setupNavigationBar() {
     
        setupTitleView()
        
    }
    /**
        设置导航栏的titleView
    */
    private func setupTitleView()
    {
        
        var childViewControllerTitles = [String]()
        
        for childVc in childViewControllers
        {
            childViewControllerTitles.append(childVc.title ?? "标题")
        
        }
        itemTitleView = TitleHeaderView(titles:childViewControllerTitles)
        itemTitleView.width = view.width * 0.5
        itemTitleView.height = 44
        itemTitleView.y = 0
        itemTitleView.tag = 99
        itemTitleView.delegate = self
        
        self.navigationItem.titleView = itemTitleView
        
    }
    
// MARK: - 设置容器内容
    private func setupContentView()
    {
        // 不要自动调整inset
        automaticallyAdjustsScrollViewInsets = false
        contentView = UIScrollView()
        contentView.showsHorizontalScrollIndicator = false
        contentView.showsVerticalScrollIndicator = false
        contentView.bounces = false
        contentView.backgroundColor = GlobalBackgroundColor
        contentView.pagingEnabled = true
        //MARK: warning
        contentView?.frame = ScreenBounds
        contentView.delegate = self
      
        view.insertSubview(contentView, atIndex: 0)
        contentView?.contentSize = CGSizeMake((contentView?.width)! * CGFloat(self.childViewControllers.count), 0)
        
        scrollViewDidEndDecelerating(contentView)

    }
// MARK: - titleHeaderViewDelegate
    func titleHeaderView(titleHeaderView: TitleHeaderView, didClickBtn btn: UIButton, forIndex index: Int)
    {
        contentView?.setContentOffset(CGPointMake((contentView?.width)! * CGFloat(index), 0), animated: true)
    }
}

/// MARK: UIScrollViewDelegate
extension HomeViewController: UIScrollViewDelegate
{
    func scrollViewDidEndDecelerating(scrollView: UIScrollView)
    {
        if scrollView == contentView
        {
            // 当前的索引
            let index:NSInteger =  NSInteger(scrollView.contentOffset.x / scrollView.width)
            
            // 取出子控制器
            let vc = self.childViewControllers[index];
            vc.view.x = scrollView.contentOffset.x
            vc.view.y = 0 // 设置控制器view的y值为0(默认是20)
            vc.view.height = scrollView.height // 设置控制器view的height值为整个屏幕的高度(默认是比屏幕高度少个20)
            scrollView.addSubview(vc.view)
            
            itemTitleView?.clickBtnWithIndex(index)
        }
    }
    
        func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView)
        {
            if scrollView == contentView
            {
                scrollViewDidEndDecelerating(contentView)
//                // 当前的索引
//                let index:NSInteger =  NSInteger(scrollView.contentOffset.x / scrollView.width)
//                
//                // 取出子控制器
//                let vc = self.childViewControllers[index];
//                vc.view.x = scrollView.contentOffset.x
//                vc.view.y = 0 // 设置控制器view的y值为0(默认是20)
//                vc.view.height = scrollView.height // 设置控制器view的height值为整个屏幕的高度(默认是比屏幕高度少个20)
//                scrollView.addSubview(vc.view)
            }
        }
}



